/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.commons.util.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;

/**
 * A service lookup utility that use the Java ServiceLoader.
 * @author Yann D'Isanto
 */
public final class ServiceLookup {

    private ServiceLookup() {
    }

    /**
     * Looks up and returns a service of the specified type.
     * @param serviceType the service type.
     * @return the service instance or null if no service is found.
     */
    public static <T> T lookup(Class<T> serviceType) {
        Class<T> serviceImpl = null;
        return lookup(serviceType, serviceImpl);
    }
    
    /**
     * Looks up and return a service of the specified type and the specified 
     * implementation type.
     * @param serviceType the service type.
     * @param serviceImpl the service implementation type.
     * @return the service instance or null if no service is found.
     */
    public static <T, U extends T> U lookup(Class<T> serviceType, Class<U> serviceImpl) {
        ServiceLoader<T> serviceLoader = ServiceLoader.load(serviceType);
        for (Iterator<T> iterator = serviceLoader.iterator(); iterator.hasNext();) {
            T t = iterator.next();
            if(serviceImpl == null || serviceImpl.isAssignableFrom(t.getClass())) {
                return (U) t;
            }
        }
        return null;
    }
    
    /**
     * Looks up and returns the list of all the services of the specified type.
     * @param serviceType the services type.
     * @return the services list.
     */
    public static <T> List<T> lookupAll(Class<T> serviceType) {
        Class<T> serviceImpl = null;
        return lookupAll(serviceType, serviceImpl);
    }
    
    /**
     * Looks up and returns the list of all the services of the specified type.
     * @param serviceType the services type.
     * @param serviceImpl the services implementation type.
     * @return the services list.
     */
    public static <T, U extends T> List<U> lookupAll(Class<T> serviceType, Class<U> serviceImpl) {
        List<U> services = new ArrayList<U>();
        ServiceLoader<T> serviceLoader = ServiceLoader.load(serviceType);
        for (Iterator<T> iterator = serviceLoader.iterator(); iterator.hasNext();) {
            T t = iterator.next();
            if(serviceImpl == null || serviceImpl.isAssignableFrom(t.getClass())) {
                services.add((U) t);
            }
        }
        return Collections.unmodifiableList(services);
    }
}
